#!/bin/bash
Docker_tools="docker_tools"
LibStorage_tools="LibStorage"
DebugTools="DebugTools"
Debug_std="DebugTools/standard"
Debug_stg="DebugTools/storage"
make_tools="${CPATH}/make_tools"
yum_conf="${CPATH}/repo_conf/obs-x86_64.conf"
yum_conf_storage="${CPATH}/repo_conf/obs-storage.conf"

function make_debug_tools()
{
    chroot_run "cd /home; bash make_version.sh make_debug_tools_inchroot"
}

function make_debug_tools_inchroot()
{
    rm -rf $CPATH/$DebugTools
    mkdir -p $CPATH/$Debug_std
#    mkdir -p $CPATH/$Debug_stg

    log_info "==========Start DebugTools perf_oprofile_tool=========="
    perf_tool_dir="perf_oprofile_tool"
    perf_tool_tar="perf_oprofile_tool.tar.gz"
    make_tool $WORK_DIR"repos/Euler" $make_tools/perf_oprofile_tool.rpmlist $perf_tool_dir $perf_tool_tar
    if [ -f "./$perf_tool_tar" ]; then
        create_checksum $perf_tool_tar
        cp -ap $perf_tool_tar "$perf_tool_tar"${SHA256SUM} ./$Debug_std/
 #       cp -ap $perf_tool_tar "$perf_tool_tar"${SHA256SUM} ./$Debug_stg/
        rm -f $perf_tool_tar "$perf_tool_tar"${SHA256SUM}
    else
        log_error "no $perf_tool_tar"
        exit 1
    fi

    log_info "==========start debugtools debug_tool=========="
    debug_tool_dir="debug_tool"
    debug_tool_tar="debug_tool.tar.gz"
    make_tool $WORK_DIR"repos/Euler" $make_tools/debug_tool.rpmlist $debug_tool_dir $debug_tool_tar
    if [ -f "./$debug_tool_tar" ]; then
        create_checksum $debug_tool_tar
        cp -ap $debug_tool_tar "$debug_tool_tar"${SHA256SUM} ./$Debug_std/
 #       cp -ap $debug_tool_tar "$debug_tool_tar"${SHA256SUM} ./$Debug_stg/
        rm -f $debug_tool_tar "$debug_tool_tar"${SHA256SUM}
    else
        log_error "no $debug_tool_tar"
        exit 1
    fi

#    log_info "==========Start DebugTools systemtap_run_tool====standard======"
#    systemtap_tool_dir="systemtap_run_tool"
#    systemtap_tool_tar="systemtap_run_tool.tar.gz"
#    make_tool $WORK_DIR"repos/Euler" $make_tools/systemtap_run_tool.rpmlist $systemtap_tool_dir $systemtap_tool_tar
#    if [ -f "./$systemtap_tool_tar" ]; then
#        create_checksum $systemtap_tool_tar
#        cp -ap $systemtap_tool_tar "$systemtap_tool_tar"${SHA256SUM} ./$Debug_stg/
#        rm -f $systemtap_tool_tar "$systemtap_tool_tar"${SHA256SUM}
#    else
#        log_error "no $systemtap_tool_tar"
#        exit 1
#    fi

    log_info "==========Start DebugTools vmcore_debug_tool====standard======"
    vmcore_tool_dir="vmcore_debug_tool"
    vmcore_tool_tar="vmcore_debug_tool.tar.gz"
    get_vmcoretool $WORK_DIR"repos/Euler" $make_tools/vmcore_debug_tool.rpmlist $vmcore_tool_dir $vmcore_tool_tar "standard"
    if [ -f "./$vmcore_tool_tar" ]; then
        create_checksum $vmcore_tool_tar
        cp -ap $vmcore_tool_tar "$vmcore_tool_tar"${SHA256SUM} ./$Debug_std/
        rm -f $vmcore_tool_tar "$vmcore_tool_tar"${SHA256SUM}
    else
        log_error "no $vmcore_tool_tar"
        exit 1
    fi

#    log_info "==========Start DebugTools vmcore_debug_tool====storage======"
#    vmcore_tool_dir="vmcore_debug_tool"
#    vmcore_tool_tar="vmcore_debug_tool.tar.gz"
#    get_vmcoretool $WORK_DIR"repos/Euler" $make_tools/vmcore_debug_tool.rpmlist $vmcore_tool_dir $vmcore_tool_tar "storage"
#    if [ -f "./$vmcore_tool_tar" ]; then
#        create_checksum $vmcore_tool_tar
#        cp -ap $vmcore_tool_tar "$vmcore_tool_tar"${SHA256SUM} ./$Debug_stg/
#        rm -f $vmcore_tool_tar "$vmcore_tool_tar"${SHA256SUM}
#    else
#        log_error "no $vmcore_tool_tar"
#        exit 1
#    fi

    
    RELEASEDIR=`get_release_dir`
    RELEASE_DIR="$RELEASEDIR/$SUB_RELEASE_DIR/"
    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"
    sshscp "$DebugTools" "$RELEASE_DIR"
    chmod_http
    if [ $? -ne 0 ]; then
        log_error "Failed in chmod_http"
    fi
}

function get_vmcoretool()
{
    el_repo=$1
    vmcorelst=$2
    tooldir=$3
    tooltar=$4
    product=$5

    if [ ! -f "$vmcorelst" ];then
        log_error "not found $vmcorelst"
        return 1
    fi
    rm -rf $tooldir
    mkdir $tooldir
    mkdir $tooldir/tmp
    #python $TOOLS/repo_maker.py -f $vmcorelst -d $tooldir/tmp -p "${OBSSERVER_IP}:82"
    yum clean all -c $yum_conf
    yum clean all -c $yum_conf_storage
    if [ "x$product" = "xstandard" ];then
        yumdownloader -c $yum_conf --destdir=$tooldir/tmp `cat $vmcorelst | tr '\n' ' '`
    elif [ "x$product" = "xstorage" ];then
        yumdownloader -c $yum_conf_storage --destdir=$tooldir/tmp `cat $vmcorelst | tr '\n' ' '`
    fi
    
    if [ $? -ne 0 ]; then
        log_error "when get vmcore debug rpm failed"
        return 1
    fi
    cd $tooldir
    cd tmp

    #vmcore_list=$(ls ./Euler/*.rpm)
    vmcore_list=$(ls ./*.rpm)
    if [ $? -ne 0 ];then
        echo "mktool.sh: function [get_vmcoretool] [$LINENO]: rpm is not exist. please download it yourself." && return 1
    fi
    for rpm in ${vmcore_list[*]}
    do
        rpm2cpio $rpm | cpio -id
        rm -rf $rpm
    done
    
    cp -p usr/bin/crash ../
    cp -p usr/lib/debug/lib/modules/*/vmlinux ../
    cd -
    rm -rf tmp
    tar -zcf ../$tooltar *
    cd ..
    rm -rf $tooldir
}

function make_tool()
{
    el_repo=$1
    rpmlst=$2
    tooldir=$3
    tooltar=$4

    if [ ! -f "$rpmlst" ];then
        log_error "not found $rpmlst"
            return 1
    fi

    rm -rf $tooldir
    mkdir $tooldir
    #python $TOOLS/repo_maker.py -f $rpmlst -d $tooldir -p "${OBSSERVER_IP}:82"
    yum clean all -c $yum_conf
    yumdownloader -c $yum_conf --destdir=$tooldir `cat $rpmlst | tr '\n' ' '`

    if [ $? -ne 0 ]; then
        log_error "when download rpm failed"
            return 1
    fi
    cd $tooldir

    #rpm_list=$(ls ./Euler/*.rpm)
    rpm_list=$(ls ./*.rpm)
    if [ $? -ne 0 ];then
        echo "mktool.sh: function [make_tool] [$LINENO]: rpm is not exist. please download it yourself." && return 1
    fi
    for rpm in ${rpm_list[*]}
    do
            rpm2cpio $rpm | cpio -id
        rm -rf $rpm
    done

    rm -rf Euler

    rm -rf usr/share/doc usr/share/man
    if [ -d bin ];then
        if [ ! -d usr/bin ];then
                mkdir -p usr/bin
        fi
        cp -ap bin/* usr/bin/
        rm -rf bin
    fi
    if [ -d sbin ];then
        if [ ! -d usr/sbin ];then
                mkdir -p usr/sbin
        fi
        cp -ap sbin/* usr/sbin/
        rm -rf sbin
    fi
    if [ -d lib ];then
        if [ ! -d usr/lib ];then
                mkdir -p usr/lib
        fi
        cp -ap lib/* usr/lib/
        rm -rf lib
    fi
    if [ -d lib64 ];then
        if [ ! -d usr/lib64 ];then
                mkdir -p usr/lib64
        fi
        cp -ap lib64/* usr/lib64/
        rm -rf lib64
    fi

    tar -zcf ../$tooltar *
    cd ..
    rm -rf $tooldir
}

function make_other_tools()
{
    chroot_run "cd /home; bash make_version.sh make_other_tools_inchroot"
}

function make_other_tools_inchroot()
{
    log_info "==========Start get_kernel_rpm=========="
    #LIST="DebugTools.lst  DockerStack.lst  ServerLess.lst"
    LIST="DockerStack.lst"
    mkdir $Docker_tools; cd $Docker_tools
    yum clean all -c $yum_conf
    yum list --installroot=${CPATH}/tmp available -c $yum_conf | awk '{print $1}' > ava_lst
    for rpmlist in $LIST
    do
        for rname in `cat $make_tools/$rpmlist`
        do
           if ! grep -w "^$rname" ava_lst > /dev/null 2>&1; then
                log_error "can not find $rname failed"
            fi
        done
        rpm_dir=`echo $rpmlist | sed 's/.lst//g'`
        mkdir $rpm_dir
        yumdownloader -c $yum_conf --disablerepo=obs-7 --destdir=$rpm_dir `cat $make_tools/$rpmlist | tr '\n' ' '`
    done
    pushd DockerStack
    wget -q -r -nd -np -k -L -p -A "docker-engine-[0-9]*.rpm" "http://euleros-obs-repo.huawei.com:82/EulerOS:/V3R1:/GENERAL/standard_x86_64/x86_64/"
    wget -c -r -nd -np -k -L -p -A "docker-engine-selinux-[0-9]*.rpm" "http://euleros-obs-repo.huawei.com:82/EulerOS:/V3R1:/GENERAL/standard_x86_64/noarch/"
    popd
    for dir in `ls ./`
    do
        if [ -d $dir ]; then
            cd $dir
            for file in `ls ./`
            do
                create_checksum $file
            done
            cd ..
        fi
    done
    cd ..

    RELEASEDIR=`get_release_dir`
    RELEASE_DIR="${RELEASEDIR}/${SUB_RELEASE_DIR}"
    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"
    sshscp "${Docker_tools}/DockerStack" "$RELEASE_DIR"
#    sshscp "${Docker_tools}/DebugTools" "$RELEASE_DIR"
    chmod_http
    if [ $? -ne 0 ]; then
        log_error "Failed in chmod_http"
    fi
    #if echo "$CI_PROJECT" | grep '_gcov'; then
    #    curl "${JENKINS_URL}/EulerOS_V200R007C00_GCOV_docker_suite_compile/build?token=xdf"
    #else
    #    curl "${JENKINS_URL}/EulerOS_V200R007C00_docker_suite_compile/build?token=xdf"
    #fi
}

function make_tools_LibStorage()
{
    chroot_run "cd /home; bash make_version.sh make_tools_LibStorage_inchroot"
}

function make_tools_LibStorage_inchroot()
{
    log_info "==========Start get_LibStorage_rpm=========="
    #LIST="DebugTools.lst  DockerStack.lst  ServerLess.lst"
    LIST="LibStorage.lst"
    mkdir $LibStorage_tools; cd $LibStorage_tools
    yum clean all -c $yum_conf
    yum list --installroot=${CPATH}/tmp available -c $yum_conf | awk '{print $1}' > ava_lst
    for rpmlist in $LIST
    do
        for rname in `cat $make_tools/$rpmlist`
        do
           if ! grep -w "^$rname" ava_lst > /dev/null 2>&1; then
                log_error "can not find $rname failed"
            fi
        done
        rpm_dir=`echo $rpmlist | sed 's/.lst//g'`
        mkdir $rpm_dir
        yumdownloader -c $yum_conf --disablerepo=obs-7 --destdir=$rpm_dir `cat $make_tools/$rpmlist | tr '\n' ' '`
    done
    for dir in `ls ./`
    do
        if [ -d $dir ]; then
            cd $dir
            for file in `ls ./`
            do
                create_checksum $file
            done
            cd ..
        fi
    done
    mkdir release debug tools
    mv $LibStorage_tools/*-dbg*.rpm ./debug
    mv $LibStorage_tools/*-mgr-tools*.rpm  ./release
    mv $LibStorage_tools/*-tools*.rpm ./tools
    mv $LibStorage_tools/*.rpm  ./release
    cd ..

    RELEASEDIR=`get_release_dir`
    RELEASE_DIR="${RELEASEDIR}/${SUB_RELEASE_DIR}/$LibStorage_tools"
    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"
    sshscp "$LibStorage_tools/release" "$RELEASE_DIR"
    sshscp "$LibStorage_tools/debug" "$RELEASE_DIR"
    sshscp "$LibStorage_tools/tools" "$RELEASE_DIR"
    chmod_http
    if [ $? -ne 0 ]; then
        log_error "Failed in chmod_http"
    fi
}

function make_container_tools()
{
    chroot_run "cd /home; bash make_version.sh make_container_tools_inchroot"
}

function make_container_tools_inchroot
{
    RELEASEDIR=`get_release_dir`
    b_version=`echo $RELEASEDIR | awk -F '_' '{print $NF}' | awk -F '-' '{print $1}'`
    tar_name="elara_${b_version}_binary"

    mkdir -p rpm_dir_tmp
    rm -rf rpm_dir_tmp/*
    cd rpm_dir_tmp
    wget -q -c -r -nd -np -k -L -p -A "elara-1.*" ${obs_docker_url}x86_64/
    rpm2cpio elara-1.*.rpm | cpio -div
    cd -

    if [ -d $tar_name ];then
        rm -rf ${tar_name}*
    fi
    cp -a ./rpm_dir_tmp/var/lib/elara-tar $tar_name
    for line in `ls $tar_name`
    do
        if [ -d ${tar_name}/${line} ];then
            chmod 750 ${tar_name}/${line}
        fi
    done
    chmod 750 ${tar_name}
    tar -cf $tar_name.tar ./$tar_name
    sha256sum $tar_name.tar > $tar_name.tar${SHA256SUM}

    RELEASE_DIR="${RELEASEDIR}/${SUB_RELEASE_DIR}/DockerStack"
    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"
    sshscp "$tar_name.tar $tar_name.tar${SHA256SUM}" "${RELEASE_DIR}"
    chmod_http
    if [ $? -ne 0 ]; then
        log_error "Failed in chmod_http"
    fi
}
